#include "stdafx.h"
#include "macroSwitcher.h"
#include "qalgorithms.h"

namespace{
    const int WIDTH = 128;
	const int HEIGHT = 28;
	const int MIN_TAB_W = 60;
	const QColor bg_Selected_and_Active(129, 165, 18);
	const QColor bg_Selected_and_NoneActive(129, 165, 18, 200);
	const QColor bg_Unselected_and_Active(99, 135, 184);
	const QColor bg_Unselected_and_NoneActive(208, 217, 216);
}

macroSwitcher::macroSwitcher(QWidget* parent)
    : QWidget(parent)
	, activeIndex(0)
{
    this->setMouseTracking(true);
    this->setFocusPolicy(Qt::ClickFocus);
    this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
}

void macroSwitcher::addTab(const QString &name, const bool &editable, const QString &default_value){
	tab_t *tab = new tab_t;
	tab->name = name;
	tab->editable = editable;
	tab->value = default_value;
	tab->isMouseOver = false;
	tabs.push_back(tab);
}

QSize macroSwitcher::minimumSizeHint() const{
    return QSize(WIDTH, HEIGHT);
}

void macroSwitcher::leaveEvent(QEvent* /*event*/){
	foreach(tab_t *tab, tabs)
		tab->isMouseOver = false;
	this->repaint();
}

void macroSwitcher::mouseMoveEvent(QMouseEvent* event){
	foreach(tab_t *tab, tabs)
		tab->isMouseOver = tab->Rect.contains(event->pos());
	this->repaint();
}


void macroSwitcher::mousePressEvent(QMouseEvent* event){
	if (change_rect.contains(event->pos()))
		emit changeClicked(activeIndex);
	for(int i = 0; i < tabs.size(); ++i)
		if (tabs[i]->Rect.contains(event->pos())){
			activeIndex = i;
			emit currentTabChanged(activeIndex);
			this->repaint();
			break;
		}
}

void macroSwitcher::setTabValue(const int &index, const QString &value){
	tabs[index]->value = value;
}

void macroSwitcher::paintEvent(QPaintEvent* event){
    WIDTH = event->rect().width();
    QPainter painter(this);
    const QBrush shadow = palette().shadow();
    const QBrush line = Qt::black;
    const QBrush light = palette().light();
	QFont font;
	const double TAB_W = (WIDTH - 1) / tabs.size();
	double left_corner = 0;
	change_rect = QRectF();
	for(int i = 0; i < tabs.size(); ++i){
		tab_t *tab = tabs[i];
		QRectF tab_rect(left_corner, 0, TAB_W, HEIGHT - 1);
		if (i == activeIndex){
			painter.setBrush(tab->isMouseOver ? bg_Selected_and_Active : bg_Unselected_and_Active);
		}else{
			painter.setBrush(tab->isMouseOver ? bg_Selected_and_NoneActive : bg_Unselected_and_NoneActive);
		}	
		painter.setPen(shadow.color());
		painter.drawRect(tab_rect);
		painter.setPen(line.color());
		font.setBold(i == activeIndex);
		painter.setFont(font);
		if (!tab->editable){
			painter.drawText(tab_rect, Qt::AlignCenter,  tab->name);
		}else{	
			painter.drawText(tab_rect.normalized().adjusted(0, 1, 0, -tab_rect.height() / 2), Qt::AlignCenter,  tab->name);
			font.setPointSize(7);
			font.setBold(false);
			painter.setFont(font);
			if (i == activeIndex && tab->isMouseOver){
				change_rect = QRectF(tab_rect.x() + tab_rect.width() / 2 - 25, tab_rect.y() + tab_rect.height() / 2 + 1, 50, tab_rect.height() / 2 - 3);
				painter.setPen(shadow.color());
				painter.setBrush(bg_Unselected_and_Active);
				painter.drawRoundedRect(change_rect, 1, 1);
				painter.setPen(line.color());
				painter.drawText(change_rect, Qt::AlignCenter,  "change");
			}else{
				painter.drawText(tab_rect.normalized().adjusted(0, 0, 0, tab_rect.height() / 2), Qt::AlignCenter,  tab->value);
			}

			
			font.setPointSize(8);
			painter.setFont(font);	
		}
		left_corner += TAB_W;
		tab->Rect = tab_rect;
	}
}

QString macroSwitcher::getTabName(const int &index) const{
	return tabs[index]->name;
}